package actions;

//~--- non-JDK imports --------------------------------------------------------

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import dto.*;
import client.*;
//~--- JDK imports ------------------------------------------------------------

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class AddPostAction extends org.apache.struts.actions.DispatchAction {
    public AddPostAction() {}

    public ActionForward add(ActionMapping mapping, ActionForm form, HttpServletRequest request,
                                 HttpServletResponse response)
            throws Exception {
        ActionForward forward = new ActionForward();
        TopicsPosts model = new TopicsPosts();
    	Posts post = new Posts();
    	Topics topic = new Topics();//this must be get from request
        String topicID =  request.getParameter("topicid");
        System.out.println("the topic id in add post action = "+topicID);
    	String content = ((beans.AddPostActionForm)form).getContent();
    	JfMembers memb = (JfMembers)request.getSession().getAttribute("userBean");
    	post.setJfMembers(memb);
    	topic.setTopicid(Integer.parseInt(topicID) - new Integer(1));
    	topic.setNumofposts(new Integer(0));
    	post.setTopics(topic);
        post.setContent(content);
    	boolean added = model.addPost(post, topic);
        if(added == false)
        {
             System.out.println(".... going to baaaaaaaaaaadtopic");
            forward = mapping.findForward("badtopic");
           return forward;
        }
        else
        {
             System.out.println(".... going to goodtopic");
           //request.setAttribute("topic", new Integer(topicID));
           response.sendRedirect("viewTopicAction.do?topic="+topicID);
           //forward = mapping.findForward("goodtopic");
           return null;
        }
    	
    }

     public ActionForward edit(ActionMapping mapping, ActionForm form, HttpServletRequest request,
                                 HttpServletResponse response)
            throws Exception {
        ActionForward forward = new ActionForward();
        TopicsPosts model = new TopicsPosts();
    	Posts post = new Posts();
    	String content = ((beans.AddPostActionForm)form).getContent();
        String postID =  request.getParameter("postid");
        System.out.println("the post id in edit post action = "+postID);
    	post.setPostid(Integer.parseInt(postID));
        JfMembers memb = (JfMembers)request.getSession().getAttribute("userBean");
    	post.setJfMembers(memb);
        post.setContent(content);
        Integer topicID = model.setPost(post);
         if(topicID == null)
        {
             System.out.println(".... going to baaaaaaaaaaadtopic");
            forward = mapping.findForward("badtopic");
           return forward;
        }
        else
        {
             System.out.println(".... going to goodtopic");
          // request.setAttribute("topic", new Integer(topicID));
             response.sendRedirect("viewTopicAction.do?topic="+topicID);
           //forward = mapping.findForward("goodtopic");
           return null;
          
          
        }
    	
    }
}


//~ Formatted by Jindent --- http://www.jindent.com
